package com.soft863.tc.producer.io;

import com.soft863.tc.common.bean.DataOut;

import java.io.*;
import java.nio.charset.StandardCharsets;

/*
 * 本地文件数据输出
 * */
public class LocalFileDataOut implements DataOut {

    private PrintWriter writer;

    public LocalFileDataOut(String path) {
        setPath(path);
    }

    @Override
    public void setPath(String path) {
        try {
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Object obj) throws IOException {
        write(obj.toString());
    }

    @Override
    public void write(String str) throws IOException {
        writer.println(str);
        writer.flush();
    }

    @Override
    public void close() throws IOException {
        if (writer != null) {
            writer.close();
        }
    }
}
